﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using ShanjinPDAUI.ModelEntity;
namespace ShanjinPDAUI.Data
{
    public class BusinessDepartmentServer
    {

        public BusinessDepartmentServer()
        {
           // _httpClientFactory = httpClientFactory;
        }
        public async Task< IEnumerable<BusinessDepartmentEntitycs>> GetBusinesses() 
        {
            using (HttpClientHandler clientHandler = new HttpClientHandler())
            {
                clientHandler.ServerCertificateCustomValidationCallback = (message, cert, chain, error) => true;
                using (HttpClient client = new HttpClient(clientHandler))
                {
                    var request = new HttpRequestMessage(HttpMethod.Get,
                  $"{AppSettingsJson.GetAppSettings().GetSection("ConnectionUrl")["DefaultConnection"]}/api/BusinessDepartment");
                    request.Headers.Add("Accept", "text / plain");
                    //var client = _httpClientFactory.CreateClient();
                    try
                    {
                        var response = await client.SendAsync(request);
                        if (response.IsSuccessStatusCode)
                        {
                            using var responseStream = await response.Content.ReadAsStreamAsync();
                            var result = await JsonSerializer.DeserializeAsync
                                <IEnumerable<BusinessDepartmentEntitycs>>(responseStream);
                            return result;
                        }
                        else
                        {
                            return null;
                            //  getBranchesError = true;
                        }

                    }
                    catch (Exception e)
                    {
                        return null;
                    }
                }



                //    var request = new HttpRequestMessage(HttpMethod.Get,
                //  $"{AppSettingsJson.GetAppSettings().GetSection("ConnectionUrl")["DefaultConnection"]}/api/BusinessDepartment");
                //request.Headers.Add("Accept", "text / plain");
                //var client = _httpClientFactory.CreateClient();



                //try
                //{
                //    var response = await client.SendAsync(request);
                //    if (response.IsSuccessStatusCode)
                //    {
                //        using var responseStream = await response.Content.ReadAsStreamAsync();
                //        var result = await JsonSerializer.DeserializeAsync
                //            <IEnumerable<BusinessDepartmentEntitycs>>(responseStream);
                //        return result;
                //    }
                //    else
                //    {
                //        return null;
                //        //  getBranchesError = true;
                //    }

                //}
                //catch (Exception e)
                //{
                //    string outstring = "";
                //    return null;
                //}
            }

        }

        public async Task< bool> ADD(BusinessDepartmentEntitycs businessDepartmentEntitycs)
        {

            using (HttpClientHandler clientHandler = new HttpClientHandler())
            {
                clientHandler.ServerCertificateCustomValidationCallback = (message, cert, chain, error) => true;
                using (HttpClient client = new HttpClient(clientHandler))
                {
                    var request = new HttpRequestMessage(HttpMethod.Post,$"{AppSettingsJson.GetAppSettings().GetSection("ConnectionUrl")["DefaultConnection"]}/api/BusinessDepartment");
                    request.Headers.Add("Accept", "text / plain");
                    request.Content = new StringContent(JsonSerializer.Serialize<BusinessDepartmentEntitycs>(businessDepartmentEntitycs), Encoding.UTF8, "application/json");

                    try
                    {
                        var response = await client.SendAsync(request);
                        if (response.IsSuccessStatusCode)
                        {
                            using var responseStream = await response.Content.ReadAsStreamAsync();
                            var result = await JsonSerializer.DeserializeAsync
                                <bool>(responseStream);
                            return result;
                        }
                        else
                        {
                            return false;
                            //  getBranchesError = true;
                        }
                    }
                    catch (Exception e)
                    {

                        return false;
                    }
                }
            }



        }


        public async Task<bool> Delete(int id) 
        {
            //  https://localhost:7001/api/BusinessDepartment/13
            using (HttpClientHandler clientHandler = new HttpClientHandler())
            {
                clientHandler.ServerCertificateCustomValidationCallback = (message, cert, chain, error) => true;
                using (HttpClient client = new HttpClient(clientHandler))
                {
                    var request = new HttpRequestMessage(HttpMethod.Delete,$"{AppSettingsJson.GetAppSettings().GetSection("ConnectionUrl")["DefaultConnection"]}/api/BusinessDepartment/{id}");
                    request.Headers.Add("Accept", "text / plain");
                    //request.Content = new StringContent(JsonSerializer.Serialize<BusinessDepartmentEntitycs>(businessDepartmentEntitycs), Encoding.UTF8, "application/json");
                    try
                    {
                        var response = await client.SendAsync(request);
                        if (response.IsSuccessStatusCode)
                        {

                            using var responseStream = await response.Content.ReadAsStreamAsync();
                            var result = await JsonSerializer.DeserializeAsync
                                <bool>(responseStream);
                            return result;
                        }
                        else
                        {
                            return false;
                            //  getBranchesError = true;
                        }
                    }
                    catch (Exception e)
                    {

                        return false;
                    }
                }
            }

     

        }

        public async Task<bool> UpData(BusinessDepartmentEntitycs businessDepartmentEntitycs) 
        {

            using (HttpClientHandler clientHandler = new HttpClientHandler())
            {
                clientHandler.ServerCertificateCustomValidationCallback = (message, cert, chain, error) => true;
                using (HttpClient client = new HttpClient(clientHandler))
                {
                    var request = new HttpRequestMessage(HttpMethod.Put,$"{AppSettingsJson.GetAppSettings().GetSection("ConnectionUrl")["DefaultConnection"]}/api/BusinessDepartment");
                    request.Headers.Add("Accept", "text / plain");
                    request.Content = new StringContent(JsonSerializer.Serialize<BusinessDepartmentEntitycs>(businessDepartmentEntitycs), Encoding.UTF8, "application/json");
                    try
                    {
                        var response = await client.SendAsync(request);
                        if (response.IsSuccessStatusCode)
                        {
                            using var responseStream = await response.Content.ReadAsStreamAsync();
                            var result = await JsonSerializer.DeserializeAsync
                                <bool>(responseStream);
                            return result;
                        }
                        else
                        {
                            return false;
                            //  getBranchesError = true;
                        }
                    }
                    catch (Exception e)
                    {

                        return false;
                    }
                }
            }
       

        }
    }
}
